package com.oracle.lingshangejia.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;


import com.oracle.lingshangejia.Constant;

import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.activity.ExpertVideoActivity;
import com.oracle.lingshangejia.activity.FieledMissionsDetailsActivity;
import com.oracle.lingshangejia.activity.HealthCheckArchivesActivity;
import com.oracle.lingshangejia.activity.MessageActivity;
import com.oracle.lingshangejia.activity.SeekMedicineActivity;
import com.oracle.lingshangejia.activity.SignActivity;

import com.oracle.lingshangejia.activity.VisitRecordActivity;
import com.oracle.lingshangejia.activity.special.LaoNianRenDetailActivity;
import com.oracle.lingshangejia.adapter.HomeAdapter;
import com.oracle.lingshangejia.base.BaseFragment;
import com.oracle.lingshangejia.bean.BannerBean;
import com.oracle.lingshangejia.bean.SeekMedicineBean;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.SpUtils;
import com.oracle.lingshangejia.view.GlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.Transformer;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class HomeFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener,View.OnClickListener {
    private LinearLayout ll_sign;
    private LinearLayout ll_visitrecode; //随机记录
    private LinearLayout ll_videos;  //专家视频
    private LinearLayout ll_field_missions;//外勤任务
    private LinearLayout ll_seekmedicine;//寻医问药S
    private Banner home_banner;
    private ArrayList<Integer> imagePath;
    private RecyclerView home_recycleview;
    private HomeAdapter homeAdapter;
    private ImageButton iv_home_message;//消息
    private List<SeekMedicineBean.DataBean> mDataBean;
    @Override
    protected void initData() {
        getNetHome();
    }
  //获取轮播
    private void getNetHome() {
        HashMap<Object, Object> map = new HashMap<>();
       // map.put("code", "04102");
       // OkHttpUtils.getNetWork(getActivity(), Constant.baseurl+"online/sylbList",map,handler);
      /*  OkgoNetwork.<BannerBean>post(getContext(), Constant.Banner, map)
        .compose(RxUtils.io_main()).subscribe(new ObserverOnNext<BannerBean>() {
            @Override
            public void onNext(BannerBean bannerBean) {
                System.out.println(bannerBean.getMsg()+"");
            }
        });*/


        int mark= (int) SpUtils.get(getContext(),"mark",0);

        map.put("mark",mark);
        JSONObject jsonObject = new JSONObject(map);
        OkGo.<String>post(Constant.baseurl+"open/sylbList")
                .tag(this)
                .upJson(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        dismissLoading();
                        Gson gson=new Gson();
                        BannerBean bean=gson.fromJson(response.body().toString(), BannerBean.class);
                        if(bean.getCode()==0) {
                            ArrayList<String> list = new ArrayList<>();
                            //   设置图片集合
                            for (int i = 0; i < bean.getData().size(); i++) {
                                list.add("http://wjw.hljcwkj.com" + bean.getData().get(i));
                            }
                            home_banner.setBannerAnimation(Transformer.CubeIn);

                            home_banner.setImages(list);

                            home_banner.start();
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        dismissLoading();
                        MyToast.show("链接服务器失败");
                        }
                });

    }

    @Override
    public void onResume() {
        super.onResume();
        getNetSeekMedicine();
    }

    @SuppressLint("HandlerLeak")
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what){
                case 1:
                    String s= (String) msg.obj;
                    try{
                        Gson gson=new Gson();
                        BannerBean bean=gson.fromJson(s, BannerBean.class);
                        if(bean.getCode()==0){
                            ArrayList<String> list=new ArrayList<>();
                            //   设置图片集合
                            for(int i=0;i<bean.getData().size();i++){
                                list.add("http://wjw.hljcwkj.com"+bean.getData().get(i));
                            }
                            home_banner.setImages(list);
                            home_banner.setBannerAnimation(Transformer.CubeIn);
                            //banner设置方法全部调用完毕时最后调用
                            home_banner.start();
                        }else {
                            Toast.makeText(getActivity(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                        }

                    }catch (Exception e){
                        e.printStackTrace();
                    }

                    break;
                case 2: //连接上服务器，错误
                    String err= (String) msg.obj;
                    Toast.makeText(getActivity(),err,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(getActivity(),"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(getActivity(),"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;

            }

        }
    };
    @Override
    protected void initView() {
       /* imagePath=new ArrayList<>();
        imagePath.add(R.drawable.timg);
        imagePath.add(R.drawable.timg1);*/
        //设置图片加载器
        home_banner.setImageLoader(new GlideImageLoader());
       /* home_banner.setImages(imagePath);
//                    //banner设置方法全部调用完毕时最后调用
        home_banner.start();*/

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        //设置布局管理器
        home_recycleview.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper. VERTICAL);
        homeAdapter=new HomeAdapter(mDataBean);
        //设置adapter
        home_recycleview.setAdapter(homeAdapter);
        homeAdapter.setOnItemClickListener(this);
        showLoading();

    }

    @Override
    protected void onBindViewBefore(View view) {
        ll_sign=view.findViewById(R.id.ll_sign);
        ll_visitrecode=view.findViewById(R.id.ll_visitrecode);
        home_banner=view.findViewById(R.id.home_banner);
        home_recycleview=view.findViewById(R.id.home_recycleview);
        ll_videos=view.findViewById(R.id.ll_videos);
        //ll_field_missions=view.findViewById(R.id.ll_field_missions);
        ll_seekmedicine=view.findViewById(R.id.ll_seekmedicine);
        iv_home_message=view.findViewById(R.id.iv_home_message);
        ll_sign.setOnClickListener(this);
        ll_visitrecode.setOnClickListener(this);
        ll_videos.setOnClickListener(this);
      //  ll_field_missions.setOnClickListener(this);
        ll_seekmedicine.setOnClickListener(this);
        iv_home_message.setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_visitrecode: //随访记录S

               // startActivity(new Intent(getActivity(), HealthCheckArchivesActivity.class));
                Intent intent = new Intent(getActivity(), VisitRecordActivity.class);
                intent.putExtra("id", 0);
                startActivity(intent);
                break;
            case R.id.ll_sign:  //签约
                startActivity(new Intent(getActivity(), SignActivity.class));
               // startActivity(new Intent(getActivity(), AntenatalArchivesActivity.class));
                break;
            case R.id.ll_videos: //专家视频

                startActivity(new Intent(getActivity(),ExpertVideoActivity.class));
                break;
            /*case R.id.ll_field_missions: //外勤任务
                startActivity(new Intent(getActivity(),OutSideTaskActivity.class));
                break;*/
            case R.id.ll_seekmedicine:  //寻医问药
                startActivity(new Intent(getActivity(), SeekMedicineActivity.class));
                //startActivity(new Intent(getActivity(), ChildrenArchivesActivity.class));
                break;
            case R.id.iv_home_message:  //消息
                startActivity(new Intent(getActivity(),MessageActivity.class));
                break;
        }
    }

    //寻医问药
    private void getNetSeekMedicine(){
        int userid= (int) SpUtils.get(getContext(),"ysid",0);
        HashMap<Object,Object> map = new HashMap<>();
        map.put("userId",userid);
        OkHttpUtils.getNetWork(getContext(), Constant.baseurl+"online/xywy/fqwtList",map,seekhandler);
    }
    @SuppressLint("HandlerLeak")
    private Handler seekhandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    try{
                        String s= (String) msg.obj;
                        Gson gson=new Gson();
                        SeekMedicineBean bean=gson.fromJson(s,SeekMedicineBean.class);
                        if(bean.getCode()==0) {
                            mDataBean = bean.getData();
                            homeAdapter.replaceData(bean.getData());
                        }else{
                            Toast.makeText(getContext(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(getContext(),error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(getContext(),"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(getContext(),"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent=new Intent(getContext(),FieledMissionsDetailsActivity.class);
                intent.putExtra("xywyId",mDataBean.get(position).getId());
                startActivity(intent);
    }
}
